package com.ebelter.seneo.ui.views;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.Rect;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;

import com.ebelter.btcomlib.utils.DipUtil;
import com.ebelter.btcomlib.utils.log.LogUtils;
import com.ebelter.seneo.R;

/**
 * 等级器
 */

public class LevelView extends View {


    String TAG = "LevelView";

    public LevelView(Context context) {
        this(context,null);
    }

    public LevelView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        init(context);
    }


    int mWidth;//整个控件的宽度
    int mHeight;//整个控件的高度


    Paint mPaint;
    public int dp1;



    private float[] middleValues;
    private int[] colors;
    private String[] buttomtitles;

    private Bitmap wujiaoxingBitmap;
    private Rect srcWujiaoxingBitmap;
    private Rect destWujiaoxingBitmap;

    public void setDisInfo(float[] middleValues,int[] colors,String[] buttomtitles){
        this.middleValues=middleValues;
        this.colors=colors;
        this.buttomtitles=buttomtitles;
       // invalidate();
    }

    private float minValuse;
    private float maxValuse;

    public void setMinMaxValuse(float minValuse,float maxValuse) {
        this.minValuse = minValuse;
        this.maxValuse = maxValuse;
    }

    private float displayValue;
    public void displayValue(float value){
        this.displayValue=value;
        invalidate();
    }



    private void init(Context context) {
        mPaint = new Paint(Paint.ANTI_ALIAS_FLAG);//抗锯齿
        mPaint.setStrokeWidth(dp1);
        dp1 = DipUtil.dip2px(context,1);
    }


    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(w, h, oldw, oldh);
        initBaseData();
    }



    private void initBaseData() {
        mWidth = getMeasuredWidth();
        mHeight = getMeasuredHeight();

        wujiaoxingBitmap = BitmapFactory.decodeResource(getContext().getResources(), R.drawable.ic_wujiaoxing);
        srcWujiaoxingBitmap = new Rect(0, 0,wujiaoxingBitmap.getWidth() , wujiaoxingBitmap.getHeight());

        out("---initBaseData--mWidth = "+mWidth+"--mHeight = "+mHeight);
    }



    int textcolor1=Color.parseColor("#FF575D67");

    @Override
    protected void onDraw(Canvas canvas) {
        out("---onDraw start");

        if (middleValues==null||colors==null||buttomtitles==null||colors.length!=buttomtitles.length){
            out("---onDraw start 数据出错 退出onDraw");
            return;
        }


        mPaint.setStyle(Paint.Style.FILL);//实心
        mPaint.setStrokeWidth((float) (dp1*7));
        mPaint.setStrokeCap(Paint.Cap.ROUND);


        mPaint.setColor(colors[0]);
        canvas.drawLine(5*dp1,mHeight/2,mWidth/colors.length,mHeight/2,mPaint);
        mPaint.setColor(colors[colors.length-1]);
        canvas.drawLine(mWidth/colors.length*(colors.length-1)+5*dp1,mHeight/2,mWidth-5*dp1,mHeight/2,mPaint);

        for (int i=1;i<colors.length-1;i++){
            mPaint.setColor(colors[i]);
            mPaint.setStrokeCap(Paint.Cap.SQUARE);
            float startX = mWidth/colors.length*i;
            float stopX = mWidth/colors.length*(i+1);
            canvas.drawLine(startX,mHeight/2,stopX,mHeight/2,mPaint);
        }

        mPaint.setColor(textcolor1);
        mPaint.setTextSize(DipUtil.sp2px(getContext(),16));
        for (int j=0;j<middleValues.length;j++){
            String text = middleValues[j]+"";
            float x = mWidth/(middleValues.length+1)*(j+1)-DipUtil.getTextWidth(mPaint,text)/2;
            float y = mHeight/2-DipUtil.getTextHeight(mPaint,text)-dp1*3;
            canvas.drawText(text,x,y,mPaint);
        }
        mPaint.setTextSize(DipUtil.sp2px(getContext(),14));

        for (int k=0;k<buttomtitles.length;k++){
            String text = buttomtitles[k]+"";

            float x1 = mWidth/(buttomtitles.length)*k;
            float x2 = mWidth/(buttomtitles.length)*(k+1);
            float x=x1+(x2-x1-DipUtil.getTextWidth(mPaint,text))/2;

           // float y = mHeight/2+DipUtil.getTextHeight(mPaint,text)+dp1*8;
            float y = mHeight/2+dp1*16+dp1*8;
            canvas.drawText(text,x,y,mPaint);
        }


        //画图标

        drawWujiaoxin(canvas);

    }


    private boolean isDrawWujiaoxin = true;

    public void setDrawWujiaoxin(boolean drawWujiaoxin) {
        isDrawWujiaoxin = drawWujiaoxin;
    }

    private void drawWujiaoxin(Canvas canvas){
        if (!isDrawWujiaoxin){
            return;
        }
        float left1 = 0;
        int displayIndex = middleValues.length;

        for (int l=0;l < middleValues.length;l++){
            if (displayValue < middleValues[l]){
                displayIndex = l;
                break;
            }
        }
        out("-------displayIndex="+displayIndex);

        float OneUnit = mWidth/(middleValues.length+1);

        if (displayIndex==0){//最左边
            left1=(displayValue-minValuse)/(middleValues[0]-minValuse)*OneUnit;
        }else if (displayIndex==middleValues.length){//最右边
            left1=mWidth-((1-(displayValue-middleValues[middleValues.length-1])/(maxValuse-middleValues[middleValues.length-1]))*OneUnit);
        }else {
            left1=((displayValue-middleValues[displayIndex-1])/(middleValues[displayIndex]-middleValues[displayIndex-1])*OneUnit)+OneUnit*displayIndex;
        }

        left1=left1-wujiaoxingBitmap.getWidth()/2;



        if (left1<0){
            left1=0;
        }else if (left1>mWidth-wujiaoxingBitmap.getWidth()){
            left1=mWidth-wujiaoxingBitmap.getWidth();
        }
        int left = (int) left1;

        int top = (int) (mHeight/2-wujiaoxingBitmap.getHeight()/2);
        int right = wujiaoxingBitmap.getWidth();
        int bottom = wujiaoxingBitmap.getHeight();

        destWujiaoxingBitmap = new Rect(left,top,left+right,top+bottom);
        canvas.drawBitmap(wujiaoxingBitmap,srcWujiaoxingBitmap,destWujiaoxingBitmap,mPaint);

    }






    private void out(String msg){
        if (false){
            Log.i(TAG,msg);
        }
    }








}
